using System.Linq;
using CardPlay.Model.ActionModel;
using CardPlay.Model.CardModel;
using CardPlay.Model.GameModel;
using CardPlay.Model.GameModel.Actions;
using CardPlay.Model.GameModel.Presentation;
using CardPlay.Model.Layout;

namespace CardPlay.DemoPack.Games.SlyFoxVariants
{
    [Game("65C22D4E-58D0-43CD-918E-C32AD71445B3", LevelOfDifficulty.Medium, LevelOfSkill.MostlyChance, DurationOfPlay.MoreThan10Minutes, NumberOfDecks = 2, Family = FamilyName)]
    public class Twenty : TwentyBase
    {
        protected bool ManualDeal { get; set; }

        protected override void PrepareStockPresentation(IPresentationContext presentation, SideBySideLayout stockWasteColumn)
        {
            var stockStyle = presentation.StyleForStock(this);
            stockStyle.EmptyCue = () => EmptyPileCue.None;

            stockWasteColumn.Add(presentation.CreatePileVisual(NameOfDealTarget, stockStyle));
        }

        protected override void PrepareDealScript(DealScript script)
        {
            script.DealAcesTo(StandardPileNames.Foundations.Take(4));
            script.DealRankTo(StandardPileNames.Foundations.Skip(4).Take(4), Rank.King);
            script.DealToEach(NamesOfTableaus, "up");
            script.DealRemainingFaceDownTo(NameOfDealTarget);
        }

        protected override bool AllowedToBuildEmptyTableau(MovePileAction action)
        {
            return true;
        }

        protected override IAction QueryActionAt(PartLocation location)
        {
            if (location.Matches(NameOfDealTarget) && !ManualDeal)
            {
                var sourcePile = this[NameOfDealTarget];

                return (!sourcePile.IsEmpty && location == sourcePile.Top)
                    ? new DealOutTableauAction(this, location) 
                    : null;
            }

            return base.QueryActionAt(location);
        }

        protected override void OnTransactionEnding()
        {
            this.FillFromStockEmptyPilesMatching(AnyTableau);
        }
    }
}